class Artifact < ActiveRecord::Base
  has_many :relations, :as => :subject
  has_many :relations, :as => :object
  has_one :context
  has_one :recent
  
  def self.get_artifacts(user, context, type = nil, query = nil)
    self.get_intersection_artifacts(user, context, type, query)
  end
  
  def self.get_union_artifacts(contenxt)
    context_artifact_ids = context.collect{|c| c.artifact.id}.join(',')
    if context_artifact_ids == ''
      return Artifact.find(:all)
    else
      stmt = <<EOS
             SELECT DISTINCT a.* FROM relations r 
               JOIN artifacts a ON r.object_id = a.id WHERE r.subject_id IN (?) 
             UNION DISTINCT 
             SELECT DISTINCT a.* FROM relations r 
               JOIN artifacts a ON r.subject_id = a.id WHERE r.object_id IN (?)
EOS
      stmt.gsub!(/\?/,context_artifact_ids)
      return Artifact.find_by_sql(stmt)
    end
  end
  
  def self.get_intersection_artifacts(user, context, type = nil, query = nil)
    artifact_ids = context.collect{|c| c.artifact.id}
    context_artifact_ids = artifact_ids.join(',')
    user_component = (user.id.nil? ? "":"AND r.user_id = #{user.id}") rescue ''
    type = '1' if type.nil?
    query = '1' if query.nil?
    type = "a.type = '#{type}'" unless type == '1'
    query = "a.title LIKE '%#{query}%'" unless query == '1'
    if context_artifact_ids == ''
      # would be interesting to return most highly associated items ...
      # I guess what might be best is settings to allow re-ordering of list
      # SELECT artifacts.*, COUNT(r.object_id) AS c FROM artifacts JOIN relations r ON r.object_id = artifacts.id GROUP BY r.object_id ORDER BY c DESC
      return Artifact.find(:all, :joins => ' AS a', :conditions=>"#{type} AND #{query}")
    else
      stmt = "SELECT a.*, COUNT(a.id) as c FROM relations r  \
              JOIN artifacts a ON r.object_id = a.id OR r.subject_id = a.id  \
              WHERE (r.subject_id IN (#{context_artifact_ids}) OR r.object_id IN (#{context_artifact_ids}))  \
              AND a.id NOT IN (#{context_artifact_ids}) \
              #{user_component}  \
              AND #{type} AND #{query} \
              GROUP BY a.id HAVING c >= #{artifact_ids.size.to_s} \
              ORDER BY a.updated_at DESC"
      
      return Artifact.find_by_sql(stmt)
    end
  end
  
  def self.get_intersection_artifacts_alternate(context)
    artifact_ids = context.collect{|c| c.artifact.id}
    context_artifact_ids = artifact_ids.join(',')
    if context_artifact_ids == ''
      return Artifact.find(:all)
    else
      stmt = <<EOS
             SELECT a.*, COUNT(a.id) as c FROM relations r
              JOIN artifacts a ON r.object_id = a.id
              WHERE r.subject_id IN (?)
              GROUP BY a.id HAVING c = !
             UNION DISTINCT 
             SELECT a.*, COUNT(a.id) as c FROM relations r 
              JOIN artifacts a ON r.subject_id = a.id 
              WHERE r.object_id IN (?)
              GROUP BY a.id HAVING c = !

EOS
      stmt.gsub!(/\?/,context_artifact_ids)
      stmt.gsub!(/!/,artifact_ids.size.to_s)
      return Artifact.find_by_sql(stmt)
    end
  end
  
  
  
end
